package com.jxt.myjxtmanager;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.jxt.myjxtmanager.utils.Config;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by Administrator on 2016/1/7 0007.
 */
public class MyApplication extends Application {

    static MyApplication instance;

    public MyApplication() {
        instance = this;
    }
    public static MyApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        String currentName = getCurProcessName(getApplicationContext());
        if ("com.jxt.myjxtmanager".equals(currentName)) {
            initImageLoader(getApplicationContext());
        }

        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
//        LogInfo.isLog = true;//调试状态
    }


    /**
     * iamgeLoader 初始化
     *
     * @param context
     */
    public void initImageLoader(Context context) {

        File cacheDir = StorageUtils.getOwnCacheDirectory(context, Config.IMAGELOADER_CACHE_PATH);

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                // .memoryCacheExtraOptions(default_width, default_height) //
                // default = device screen dimensions
                .diskCacheExtraOptions(480, 800, null).threadPoolSize(5)
                        // default
                .threadPriority(Thread.NORM_PRIORITY - 2)
                        // default
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                        // default
                        // .denyCacheImageMultipleSizesInMemory()//允许缓存一张图片多个大小
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024)).memoryCacheSize(2 * 1024 * 1024)
                .memoryCacheSizePercentage(13)
                        // default
                        // .diskCacheFileCount(100)
                .memoryCache(new WeakMemoryCache())
                .diskCacheFileNameGenerator(new Md5FileNameGenerator()) // default
                .imageDownloader(new BaseImageDownloader(context)) // default
                .imageDecoder(new BaseImageDecoder(true)) // default
                .diskCache(new UnlimitedDiskCache(cacheDir))// 自定义缓存路径
                .writeDebugLogs().build();
        ImageLoader.getInstance().init(config);
    }


    /**
     * 一个获得当前进程的名字的方法
     *
     * @param context
     * @return
     */
    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
